using MicroMsg.Plugin.FaceBook;
using MicroMsg.Resource.@string;
using MicroMsg.Storage;
using MicroMsg.UI.Page;
using MicroMsg.UI.Util;
using Microsoft.Phone.Controls;
using System;
using System.Diagnostics;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Media.Imaging;
namespace MicroMsg.UI.UserContrl
{
	public class FaceBookFriendItem : UserControl
	{
		private OpenReadCompletedEventHandler imgHandler;
		public static DependencyProperty ListItemProperty;
		private string originImgSrc = "";
		internal DynamicTextBlock FriendNameTxt;
		internal Image FriendImg;
		internal TextBlock txtTips;
		internal Image addIcon;
		private bool _contentLoaded;
		public FBContact ItemObject
		{
			get
			{
				return (FBContact)base.GetValue(FaceBookFriendItem.ListItemProperty);
			}
			set
			{
				base.SetValue(FaceBookFriendItem.ListItemProperty, value);
			}
		}
		public FaceBookFriendItem()
		{
			this.InitializeComponent();
			this.imgHandler = new OpenReadCompletedEventHandler(this.w_OpenReadCompleted);
		}
		static FaceBookFriendItem()
		{
			FaceBookFriendItem.ListItemProperty = DependencyProperty.Register("ItemObject", typeof(FBContact), typeof(FaceBookFriendItem), new PropertyMetadata(null, new PropertyChangedCallback(FaceBookFriendItem.OnItemPropertyChanged)));
		}
		private static void OnItemPropertyChanged(DependencyObject dependencyObject_0, DependencyPropertyChangedEventArgs dependencyPropertyChangedEventArgs_0)
		{
			if (dependencyPropertyChangedEventArgs_0.get_Property() == FaceBookFriendItem.ListItemProperty)
			{
				(dependencyObject_0 as FaceBookFriendItem).OnListItemPropertyChanged(dependencyPropertyChangedEventArgs_0);
			}
		}
		private void RefreshItem(FBContact item)
		{
			if (item == null)
			{
				return;
			}
			this.FriendNameTxt.set_Width(190.0);
			if (item.nStatus == 1)
			{
				this.txtTips.set_Text(strings.QQFriend_Add);
				this.txtTips.set_Foreground(MMTheme.GetInstance().SNS_MSGPAGE_TIME_BRUSH);
				this.addIcon.set_Visibility(0);
			}
			else
			{
				if (item.nStatus == 0)
				{
					this.txtTips.set_Text(strings.Friend_Invite);
					this.txtTips.set_Foreground(MMTheme.GetInstance().COMMON_BRUSH);
					this.addIcon.set_Visibility(1);
				}
				else
				{
					this.txtTips.set_Text(strings.QQFriend_Added);
					this.txtTips.set_Foreground(MMTheme.GetInstance().SNS_MSGPAGE_TIME_BRUSH);
					this.addIcon.set_Visibility(1);
				}
			}
			this.FriendNameTxt.Text = FaceBookMgr.GetFaceBookShowName(item);
			BitmapImage cache = FaceBookMgr.getCache(item.nID);
			if (cache != null)
			{
				this.FriendImg.set_Source(cache);
				return;
			}
			string text = string.Format("http://graph.facebook.com/{0}/picture", item.nID);
			this.originImgSrc = text;
			WebClient webClient = new WebClient();
			webClient.add_OpenReadCompleted(this.imgHandler);
			webClient.OpenReadAsync(new Uri(text), item.nID);
		}
		private void OnListItemPropertyChanged(DependencyPropertyChangedEventArgs dependencyPropertyChangedEventArgs_0)
		{
			this.RefreshItem(dependencyPropertyChangedEventArgs_0.get_NewValue() as FBContact);
		}
		private void w_OpenReadCompleted(object sender, OpenReadCompletedEventArgs e)
		{
			ulong num = (ulong)e.get_UserState();
			BitmapImage bitmapImage = null;
			try
			{
				bitmapImage = FaceBookMgr.addCache(num, e.get_Result());
			}
			catch (Exception ex)
			{
				ex.ToString();
			}
			if (num != this.ItemObject.nID)
			{
				return;
			}
			if (bitmapImage != null)
			{
				this.FriendImg.set_Source(bitmapImage);
				return;
			}
			this.FriendImg.set_Source(new ImageSourceConverter().ConvertFromString("/Resource/Img/CommonUse/DefaultHead.png") as ImageSource);
		}
		private void Button_Click(object sender, RoutedEventArgs e)
		{
			if (this.ItemObject == null)
			{
				return;
			}
			PhoneApplicationFrame phoneApplicationFrame = Application.get_Current().get_RootVisual() as PhoneApplicationFrame;
			BasePage basePage = phoneApplicationFrame.get_Content() as BasePage;
			if (basePage == null)
			{
				return;
			}
			if (this.ItemObject.nStatus == 2)
			{
				ContactDetailViewForAll.GotoContactDitailView(this.ItemObject, false);
				return;
			}
			if (this.ItemObject.nStatus == 1)
			{
				ContactDetailViewForAll.GotoContactDitailView(this.ItemObject, false);
				return;
			}
			InviteFriends.StartInvite(basePage, this.ItemObject);
		}
		[DebuggerNonUserCode]
		public void InitializeComponent()
		{
			if (this._contentLoaded)
			{
				return;
			}
			this._contentLoaded = true;
			Application.LoadComponent(this, new Uri("/MicroMsg;component/Source/Plugin/Facebook/UI/FaceBookFriendItem.xaml", 2));
			this.FriendNameTxt = (DynamicTextBlock)base.FindName("FriendNameTxt");
			this.FriendImg = (Image)base.FindName("FriendImg");
			this.txtTips = (TextBlock)base.FindName("txtTips");
			this.addIcon = (Image)base.FindName("addIcon");
		}
	}
}
